/**
 * Created by zhangguoyu on 16/5/10.
 */

/**
 * 元数据
 * @param id
 * @constructor
 */
var Meta = function (id) {
  this.id = id;
  this.visited = 0;  // 访问量
  this.users = 0;  // 用户量
  this.newUsers = 0;  // 新增用户量
  this.playCount = 0;  // 播放次数
  this.durations = 0;  // 播放时长

  this._getIncArg = function (args) {
    var inc = 1;
    if (args.length) {
      inc = parseInt(args[0]);
    }
    return inc;
  }
};

/**
 * 增加访问数
 * @returns {Meta}
 */
Meta.prototype.incVisited = function () {
  this.visited += this._getIncArg(arguments);
  return this;
};

/**
 * 增加用户数
 * @returns {Meta}
 */
Meta.prototype.incUsers = function () {
  this.users += this._getIncArg(arguments);
  return this;
};

/**
 * 增加新增用户数
 * @returns {Meta}
 */
Meta.prototype.incNewUsers = function () {
  this.newUsers += this._getIncArg(arguments);
  return this;
};

/**
 * 增加播放次数
 * @returns {Meta}
 */
Meta.prototype.incPlayCount = function () {
  this.playCount += this._getIncArg(arguments);
  return this;
};

/**
 * 增加播放时长
 * @returns {Meta}
 */
Meta.prototype.incDurations = function () {
  this.durations += this._getIncArg(arguments);
  return this;
};

/**
 * 统计数据对象的基类
 * @constructor
 */
var Stat = function () {
  this._meta = new Meta('$total', 'total');
  this._times = [];
  this._flavors = [];
};

/**
 * 总共的统计
 * @returns {Meta}
 */
Stat.prototype.meta = function () {
  return this._meta;
};

/**
 * 获取指定时段的统计
 * @param t 时间段
 * @returns {Meta}
 */
Stat.prototype.time = function (t) {
  var i = this._times.length;
  var tag = undefined;
  while (i--) {
    if (this._times[i].id === t) {
      tag = this._times[i];
      break;
    }
  }
  if (tag === undefined) {
    tag = new Meta(t);
    this._times.push(tag);
  }
  return tag;
};

/**
 * 获取指定渠道的统计
 * @param f 渠道
 * @returns {Meta}
 */
Stat.prototype.flavor = function (f) {
  var i = this._flavors.length;
  var tag = undefined;
  while (i--) {
    if (this._flavors[i].id === f) {
      tag = this._flavors[i];
      break;
    }
  }
  if (tag === undefined) {
    tag = new Meta(f);
    this._flavors.push(tag);
  }
  return tag
};

Stat.prototype.incVisited = function () {
  var inc = 1;
  if (arguments.length > 2) {
    inc = parseInt(arguments[2]);
  }
  this.meta().incVisited(inc);
  if (arguments.length) {
    this.time(arguments[0]).incVisited(inc);
  }
  if (arguments.length > 1) {
    this.flavor(arguments[1]).incVisited(inc);
  }
  return this;
};

Stat.prototype.incNewUsers = function () {
  var inc = 1;
  if (arguments.length > 2) {
    inc = parseInt(arguments[2]);
  }
  this.meta().incNewUsers(inc);
  if (arguments.length) {
    this.time(arguments[0]).incNewUsers(inc);
  }
  if (arguments.length > 1) {
    this.flavor(arguments[1]).incNewUsers(inc);
  }
  return this;
};

Stat.prototype.incUsers = function () {
  var inc = 1;
  if (arguments.length > 2) {
    inc = parseInt(arguments[2]);
  }
  this.meta().incUsers(inc);
  if (arguments.length) {
    this.time(arguments[0]).incUsers(inc);
  }
  if (arguments.length > 1) {
    this.flavor(arguments[1]).incUsers(inc);
  }
  return this;
};

Stat.prototype.incPlayCount = function () {
  var inc = 1;
  if (arguments.length > 2) {
    inc = parseInt(arguments[2]);
  }
  this.meta().incPlayCount(inc);
  if (arguments.length) {
    this.time(arguments[0]).incPlayCount(inc);
  }
  if (arguments.length > 1) {
    this.flavor(arguments[1]).incPlayCount(inc);
  }
  return this;
};

Stat.prototype.incDurations = function () {
  var inc = 1;
  if (arguments.length > 2) {
    inc = parseInt(arguments[2]);
  }
  this.meta().incDurations(inc);
  if (arguments.length) {
    this.time(arguments[0]).incDurations(inc);
  }
  if (arguments.length > 1) {
    this.flavor(arguments[1]).incDurations(inc);
  }
  return this;
};

Stat.Meta = Meta;
module.exports = Stat;